<?php
// 获取用户信息
class GET
{
    // 参数描述
    /*{{{*/
    /*
       +---------------------------------------------------------------+
       |输入                                                           |
       +-------------------------------------------+-------------------+
       |参数                                       |描述               |
       +-------------------------------------------+-------------------+
       |'loginname'=>账户名                        |                   |
       +-------------------------------------------+-------------------+

       +---------------------------------------------------------------+
       |输出                                                           |
       +-------------------------------------------+-------------------+
       |参数                                       |描述               |
       +-------------------------------------------+-------------------+
       |'errcode'=>SOAR_ERR::OK                    |成功               |
       +-------------------------------------------+-------------------+
     */
    /*}}}*/

    public function call($request)
    {/*{{{*/
        if(($dbconn=worker::get_dbconn('db1')) === false)
            return false;

        if(self::check_para($request) == false)
        {
            return array('errcode'=>SOAR_ERR::PARA);
        }
        $loginname=trim($request['loginname']);

        //    try
        //    {
        //        $stmt=$dbconn->prepare("select uuid,".
        //                "date_format(create_ts,'%Y%m%d%H%i%s'),".
        //                "date_format(update_ts,'%Y%m%d%H%i%s'),".
        //                "loginname,nickname,login_times ".
        //                "from memb_info where loginname=:loginname");
        //        $stmt->execute(array(':loginname'=>$loginname));
        //        $row=$stmt->fetch(PDO::FETCH_NUM);
        //        //print_r($row);
        //        if(empty($row))
        //        {
        //            $response=array('errcode'=>'MEMB_NOT_FOUND');
        //        }
        //        else
        //        {
        //            $response=array('errcode'=>SOAR_ERR::OK);
        //            $paras['uuid']=>$row[0];
        //            $paras['create_ts']=>$row[1];
        //            $paras['update_ts']=>$row[2];
        //            $paras['loginname']=>$row[3];
        //            $paras['nickname']=>$row[4];
        //            $paras['login_times']=>$row[5];
        //            $response['paras']=$paras;
        //        }
        //    }
        //    catch(PDOException $e)
        //    {
        //        errlog::add("%s: worker(%d) select memb_info fail: %s",
        //                __METHOD__, worker::$worker_idx,
        //                $e->getMessage()); 
        //        return false;
        //    }

        $stmt=$dbconn->prepare(
                "select uuid,".
                "date_format(create_ts,'%Y%m%d%H%i%s'),".
                "date_format(update_ts,'%Y%m%d%H%i%s'),".
                "loginname,nickname,login_times ".
                "from memb_info where loginname=?"
                );
        if($stmt === false)
        {
            errlog::add("%s: worker(%d) prepare fail:%d %s",
                    __METHOD__, worker::$worker_idx,
                    $dbconn->errno, $dbconn->error);
            return false;
        }
        $stmt->bind_param('s', $loginname);
        if($stmt->execute() == false)
        {
            errlog::add("%s: worker(%d) mysqli_query fail:%d %s",
                    __METHOD__, worker::$worker_idx,
                    $stmt->errno, $stmt->error);
            return false;
        }
        $stmt->store_result();
        if($stmt->num_rows() == 0)
        {
            $response=array('errcode'=>'MEMB_NOT_FOUND');
        }
        else
        {
            $stmt->bind_result($uuid, $create_ts, $update_ts,
                    $loginname, $nickname, $login_times);
            $stmt->fetch();
            $response=array('errcode'=>SOAR_ERR::OK,
                    'uuid'=>$uuid,
                    'create_ts'=>$create_ts,
                    'update_ts'=>$update_ts,
                    'loginname'=>$loginname,
                    'nickname'=>$nickname,
                    'login_times'=>$login_times);
        }
        $stmt->close();

        return $response;
    }/*}}}*/

    private function check_para($request)
    {/*{{{*/
        if(!isset($request['loginname']))
        {
            errlog::add("%s: loginname unset", __METHOD__);
            return false;
        }
        $loginname=trim($request['loginname']);
        if(strlen($loginname) > misc::NAME_SIZE)
        {
            errlog::add("%s: loginname(%s) invalid",
                    __METHOD__, $loginname);
            return false;
        }

        return true;
    }/*}}}*/
}
?>
